package com.alex.cms.web.controllers;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alex.cms.domain.CmsSpecial;
import com.alex.cms.domain.CmsSpecialArticle;
import com.alex.cms.domain.CmsUser;
import com.alex.cms.service.ICmsSpecialArticleService;
import com.alex.cms.service.ICmsSpecialService;
import com.alex.cms.web.constants.CMSConstants;
import com.alex.cms.web.vo.ResultVo;
import com.alex.cms.web.vo.SpecialArticleVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bawei.common.utils.StringUtil;

/**
 * <p>
 * 专题表 前端控制器
 * </p>
 *
 * @author alex
 * @since 2019-10-24
 */
@Controller
@RequestMapping("/special")
public class CmsSpecialController {

	@Autowired
	private ICmsSpecialService specialService;

	@Autowired
	private ICmsSpecialArticleService specialArticleService;

	/**
	 * 
	 * @Title: showSpecial
	 * @Description: 展示添加页面
	 * @return
	 * @return ModelAndView
	 */
	@RequestMapping("/toAddSpecial")
	public ModelAndView showSpecial() {
		// 指定跳转的jsp页面
		ModelAndView mav = new ModelAndView("/admin/special-edit");

		CmsSpecial special = new CmsSpecial();
		mav.addObject("special", special);
		return mav;
	}

	/**
	 * 
	 * @Title: getEditSlide
	 * @Description: 展示修改页面
	 * @param specialId
	 * @return
	 * @return ModelAndView
	 */
	@RequestMapping("/toEditSpecial")
	public ModelAndView getEditSpecial(@RequestParam("specialId") Integer specialId) {
		ModelAndView mav = new ModelAndView("admin/special-edit");
		CmsSpecial special = specialService.getById(specialId);
		mav.addObject("special", special);
		return mav;
	}

	/**
	 * 
	 * @Title: editSpecial
	 * @Description: 添加/修改页面
	 * @param special
	 * @param session
	 * @return
	 * @return ModelAndView
	 */
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	public ModelAndView editSpecial(CmsSpecial special, HttpSession session) {
		CmsUser user = (CmsUser) session.getAttribute(CMSConstants.LOGIN_USER);
		ModelAndView mav = new ModelAndView("redirect:/admin/specials");
		String message = "";
		if (user == null) {
			message = CMSConstants.USER_DO_NOT_LOGIN;
			mav.setViewName("redirect:/user/login");
			mav.addObject("message", message);
			return mav;
		}
		if (!StringUtil.hasText(special.getTitle())) {
			message = CMSConstants.TITLE_IS_NOT_NULL;
			mav.addObject("message", message);
			mav.addObject("special", special);
			mav.setViewName("admin/special-edit");
			return mav;
		}
		if (special.getId() == null) {
			special.setCreated(new Date());
			specialService.save(special);
		} else {
			specialService.updateById(special);
		}
		return mav;
	}

	/**
	 * 
	 * @Title: toAddArticle
	 * @Description: 展示专题主题设置页面
	 * @param specialId
	 * @return
	 * @return ModelAndView
	 */
	@RequestMapping("/toAddArticle")
	public ModelAndView toAddArticle(@RequestParam("specialId") Integer specialId) {
		ModelAndView mav = new ModelAndView("admin/special-article");
		List<SpecialArticleVo> articles = specialArticleService.getAllArticleById(new QueryWrapper<SpecialArticleVo>().eq("csa.sid", specialId));
		CmsSpecial special = specialService.getById(specialId);
		mav.addObject("special",special);
		mav.addObject("articles", articles);
		mav.addObject("specialId", specialId);
		return mav;
	}

	/**
	 * 
	 * @Title: addArticle
	 * @Description: 添加文章到主题
	 * @param aid
	 * @param specialId
	 * @return
	 * @return ModelAndView
	 */
	@RequestMapping("/addArticle")
	public ModelAndView addArticle(@RequestParam("aid") Integer aid, @RequestParam("specialId") Integer specialId) {
		ModelAndView mav = new ModelAndView("redirect:/special/toAddArticle?specialId="+specialId);
		List<CmsSpecialArticle> cmsSpecialArticles = specialArticleService.list(new QueryWrapper<CmsSpecialArticle>().eq("aid", aid).eq("sid", specialId));
		if(!CollectionUtils.isEmpty(cmsSpecialArticles)){
			String message = CMSConstants.ARTICLE_IS_EXIST;
			mav.addObject("message", message);
			return mav;
		}
		CmsSpecialArticle specialArticle = new CmsSpecialArticle();
		specialArticle.setAid(aid);
		specialArticle.setSid(specialId);
		specialArticleService.save(specialArticle);
		return mav;
	}
	/**
	 * 
	* @Title: removeArticle
	* @Description: 从主题中移除文章
	* @param aid
	* @param sid
	* @return
	* @return ResultVo
	 */
	@RequestMapping("/removeArticle")
	@ResponseBody
	public ResultVo removeArticle(@RequestParam("aid") Integer aid, @RequestParam("sid") Integer sid) {
		ResultVo resultVo = new ResultVo();
		boolean ret = specialArticleService.remove(new QueryWrapper<CmsSpecialArticle>().eq("aid", aid).eq("sid", sid));
		resultVo.setStatus(ret == true ? CMSConstants.RESULT_STATUS_SUCCESS : CMSConstants.RESULT_STATUS_ERROR);
		resultVo.setMessage(ret == true ? CMSConstants.RESULT_DELETE_MESSAGE_SUCCESS : CMSConstants.RESULT_DELETE_MESSAGE_ERROR);
		return resultVo;
	}

}
